package com.mail.manager.modular.controller.portal;

import com.mail.manager.common.annotion.Permission;
import com.mail.manager.common.annotion.log.BussinessLog;
import com.mail.manager.common.constant.Dict;
import com.mail.manager.common.constant.factory.ConstantFactory;
import com.mail.manager.common.constant.tips.Tip;
import com.mail.manager.common.controller.BaseController;
import com.mail.manager.common.exception.BizExceptionEnum;
import com.mail.manager.common.exception.BussinessException;
import com.mail.manager.core.log.LogObjectHolder;
import com.mail.manager.core.util.ToolUtil;
import com.mail.manager.modular.model.Banner;
import com.mail.manager.modular.service.portal.IBannerService;
import com.mail.manager.modular.warpper.DeptWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 创建人： Shi Heng
 * 创建时间：2018-03-29 10:34
 * QQ：374696376
 * Description：
 */
@Controller
@RequestMapping("/portal/banner")
public class BannerController extends BaseController {
    private static String PREFIX = "/portal/banner/";

    @Resource
    IBannerService bannerService;

    /**
     * 跳转到banner列表的页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "banner.html";
    }

    /**
     * 跳转到添加banner
     */
    @RequestMapping("/banner_add")
    public String bannerAdd() {
        return PREFIX + "banner_add.html";
    }

    /**
     * 跳转到编辑banner页面
     */
    @Permission
    @RequestMapping("/banner_edit/{bannerId}")
    public String userEdit(@PathVariable Integer bannerId, Model model) {
        if (ToolUtil.isEmpty(bannerId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        Banner banner = bannerService.selectByPrimaryKey(bannerId);
        model.addAttribute(banner);
        return PREFIX + "banner_edit.html";
    }
    /**
     * 获取所有Banner列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list = bannerService.list(condition);
        return super.warpObject(new DeptWarpper(list));
    }


    /**
     * 角色新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Tip add(Banner banner, BindingResult result) {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        bannerService.insert(banner);
        return SUCCESS_TIP;
    }

    /**
     * 修改banner
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public Tip edit(Banner banner, BindingResult result) {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        bannerService.updateObjtById(banner);
        return SUCCESS_TIP;
    }

    /**
     * 删除banner
     * @param bannerId
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer bannerId) {
        bannerService.delteObject(bannerId);
        return SUCCESS_TIP;
    }
}
